<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="/layui/css/layui.css" rel="stylesheet">
    <script src="/layui/layui.js"></script>
    <script src="/layui/jquery.min.js"></script>
    <script src="/layui/axios.min.js"></script>
    <script src="/layui/vue.min.js"></script>
</head>
<body>
<div>
    <div style="margin-left: 20px;margin-right: 20px;margin-bottom: 100px">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>用户添加</legend>
        </fieldset>
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md6">
                <div class="layui-card">
                    <div class="layui-card-header">用户账号信息填写</div>
                    <div class="layui-card-body">
                        <form class="layui-form" style="margin-right: 100px">
                            <div class="layui-form-item">
                                <label class="layui-form-label">用户名</label>
                                <div class="layui-input-block">
                                    <input type="text" name="userName" lay-verify="title" autocomplete="off" placeholder="请输入用户名" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">密码</label>
                                <div class="layui-input-block">
                                    <input type="password" name="userPassword" lay-verify="pass" autocomplete="off" placeholder="请输入密码" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">用户角色</label>
                                <div class="layui-input-block">
                                    <select name="roleId" id="role" lay-verify="title" lay-filter="role">
                                        <option value=""></option>
                                    </select>
                                </div>
                            </div>
                            <div style="margin: 20px 20px;text-align: center">
                                <button class="layui-btn layui-btn-primary" lay-submit lay-filter="add">确认添加</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script>
    layui.use(['form','layer'],function () {
        var form = layui.form,
        layer = layui.layer;


        axios.get("/role/getRoleForAddUser").then(res => {
            var role = $("#role");
            role.children().remove();
            role.append('<option value=""></option>')
            for (const op of res.data.data) {
                role.append('<option value="'+op.id+'">'+'角色名称:'+op.roleName+',权限描述:' +op.roleDesc+'</option>')
            }
            form.render();
        })

        form.on('submit(add)',function (data){
            console.log(data)
            axios.post("/end/user/add",data.field).then(res => {
                layer.msg(res.data.msg);
            })
            return false
        })

        form.verify({
            title: function(value){
                if(value.length < 1){
                    return '输入项至少得1个字符啊';
                }
            }
            ,image: function (value) {
                if(value.length < 1){
                    return '灾情封面不可以无哇';
                }
            }
            ,pass: [
                /^[\S]{6,12}$/
                ,'密码必须6到12位，且不能出现空格'
            ]
        });

    })
</script>
</html>
